<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all"/>
    <script src="/layuiadmin/layui/layui.js"></script>
</head>
<body style="background-color:#fff">

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="" class="layui-form layui-form-pane" lay-filter="layui-form-pane" id="sbumitForm">
            <div class="layui-form-item">
                <label class="layui-form-label">比赛类型</label>
                <div class="layui-input-block">
                    <select name="sport_id"  lay-filter="sport_id">
                        <option value="">无</option>
                        <option value="1">足球</option>
                        <option value="2">篮球</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">所属赛事</label>
                <div class="layui-input-block">
                    <select name="match_id"  id="match_id" lay-filter="match_id" lay-search>
                        <option value="">无</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">推送标题</label>
                <div class="layui-input-block">
                    <input type="text" name="title" value="" lay-verify="required" id="input_title" class="layui-input" placeholder="最多输入十五个字符" autocomplete="off">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">推送内容</label>
                <div class="layui-input-block">
                    <textarea name="content" class="layui-textarea" lay-verify="required" placeholder="最多输入200个字符" ></textarea>
                </div>
            </div>

            <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg6" style="float: left">
                    <label class="layui-form-label">推送类型：</label>
                    <div class="layui-input-block" style="height: 35px;line-height: 35px">
                        <input type="radio" name="user" lay-verify="required" checked title="全部" value="0" >
                        <input type="radio" name="user" lay-verify="required" title="按用户ID进行推送" value="1" >
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <textarea name="user_id" class="layui-textarea"  placeholder="用户ID用,号隔开"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">推送时间</label>
                <div class="layui-input-block">
                    <input type="text" name="push_time" value="" lay-verify="required" class="layui-input" placeholder="预设推送时间" autocomplete="off" id="push_time" readonly>
                </div>
            </div>

            <div class="layui-form-item" style="margin-top: 35px">
                <div class="layui-input-block">
                    <span class="layui-btn"  lay-submit="" lay-filter="submitForm" style="width: 100%">确认添加</span>
                </div>
            </div>
        </form>
    </div>
</div>

<script>
    var matches_schedule_json = <?php echo $matches_schedule_json; ?>;
    layui.use(['form','laydate'], function () {
        $ = layui.jquery;
        var form = layui.form, layer = layui.layer, laydate = layui.laydate;

        $(this).removeAttr("lay-key");
         var push_time_laydate_conf = laydate.render({
            elem: '#push_time', //指定元素,
            type: 'datetime',
            min: '<?php echo date("Y-m-d H:i:s");?>',
        });

        form.on('select(sport_id)', function (data) {
            $("#match_id").empty();
            var html = '';
            if (data.value) {
                html += '<option value="">无</option>';
            } else {
                html += '<option value="" selected>无</option>';
            }

            for(const i in matches_schedule_json) {
                if (data.value) {
                    if (matches_schedule_json[i].sport_id == data.value) {
                        html += '<option value="' + matches_schedule_json[i].match_id + '">' + matches_schedule_json[i].comp + '</option>';
                    }
                } else {
                    html += '<option value="' + matches_schedule_json[i].match_id + '">' + matches_schedule_json[i].comp + '</option>';
                }
            }

            $("#match_id").html(html);

            form.render();
        });

        form.on('submit(submitForm)', function (obj) {
            layer.confirm('确认要添加吗？', {icon: 6}, function () {
                $.ajax({
                    type: "POST", //请求方式
                    url: '/admin/offline_push/add', //请求地址
                    dataType: "json", //返回的数据类型
                    data: $("#sbumitForm").serialize(), //数据
                    //请求成功
                    success: function (data) {
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 2000}, function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                window.parent.location.reload();
                            });
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 5000});
                        }
                    },
                    //请求失败
                    error: function (e) {

                    }
                });
            });
            return false;
        });

        form.on('select(match_id)', function (data) {
            var min = getDate(0);
            push_time_laydate_conf.config.min = {
                year: min.getFullYear(),
                month: min.getMonth(),
                date: min.getDate(),
                hours: min.getHours(),
                minutes: min.getMinutes(),
                seconds: min.getSeconds()
            };
            if (data.value) {
                if (!$("#input_title").val()) {
                    $("#input_title").val($("#match_id option:selected").text());
                }

                return false;
                var max = getDate(matches_schedule_json[data.value].match_time);
                push_time_laydate_conf.config.max = {
                    year: max.getFullYear(),
                    month: max.getMonth(),
                    date: max.getDate(),
                    hours: max.getHours(),
                    minutes: max.getMinutes(),
                    seconds: max.getSeconds()
                };
            } else {
                var max = getDate(0);
                push_time_laydate_conf.config.max = {
                    year: max.getFullYear() + 1,
                    month: max.getMonth(),
                    date: max.getDate(),
                    hours: max.getHours(),
                    minutes: max.getMinutes(),
                    seconds: max.getSeconds()
                };
            }
            form.render();
        });
    });

    function add0(m) {
        if (m < 10) {
            return '0' + m;
        }

        return m;
    }

    function getDate(time) {
        if (time) {
            time = time + 10800;
            var myDate = new Date(time * 1000);
        } else {
            var myDate = new Date();
        }

        //var year = myDate.getFullYear(); //获取当前年
        //var mon = myDate.getMonth() + 1; //获取当前月
        //var date = myDate.getDate(); //获取当前日
        //var h = myDate.getHours();//获取当前小时数(0-23)
        //var m = myDate.getMinutes();//获取当前分钟数(0-59)
        //var s = myDate.getSeconds();//获取当前秒

        return myDate;

        //return year + '-' + add0(mon) + '-' + add0(date) + ' ' + add0(h) + ':' + add0(m) + ':' + add0(s);
    }

</script>
</body>

</html>
